package flc.ast.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.MyDebugBean;
import flc.ast.bean.MyUdpCombBean;
import flc.ast.databinding.ActivityAddUdpCombBinding;
import flc.ast.utils.AppSpUtils;

public class AddUdpCombActivity extends BaseAc<ActivityAddUdpCombBinding> {

    private boolean isRandom = true;

    @Override
    protected int onCreate() {
        return R.layout.activity_add_udp_comb;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAddUdpCombBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.llRandom.setOnClickListener(this);
        mDataBinding.llDesignate.setOnClickListener(this);
        mDataBinding.tvSave.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.llRandom) {//随机
            isRandom = true;
            mDataBinding.ivRandom.setImageResource(R.drawable.xuanzhong);
            mDataBinding.ivDesignate.setImageResource(R.drawable.weixuanzhong);
        } else if (id == R.id.llDesignate) {//指定
            isRandom = false;
            mDataBinding.ivRandom.setImageResource(R.drawable.weixuanzhong);
            mDataBinding.ivDesignate.setImageResource(R.drawable.xuanzhong);
        }
        if (id == R.id.tvSave) {//保存
            String name = mDataBinding.etName.getText().toString().trim();
            String address = mDataBinding.etAddress.getText().toString().trim();
            String port = mDataBinding.etPort.getText().toString().trim();
            String ttl = mDataBinding.etTtl.getText().toString().trim();
            if (TextUtils.isEmpty(name)) {
                ToastUtils.showShort(R.string.please_input_name);
                return;
            }
            if (TextUtils.isEmpty(address)) {
                ToastUtils.showShort(R.string.please_input_address);
                return;
            }
            if (TextUtils.isEmpty(port)) {
                ToastUtils.showShort(R.string.please_input_port);
                return;
            }
            if (TextUtils.isEmpty(ttl)) {
                ToastUtils.showShort(R.string.please_input_ttl);
                return;
            }
            List<MyUdpCombBean> listSp = AppSpUtils.getUdpCombList();
            if (listSp == null) {
                listSp = new ArrayList<>();
            }
            listSp.add(new MyUdpCombBean(name, address, port, isRandom, ttl));
            AppSpUtils.saveUdpCombList(listSp);
            ToastUtils.showShort(getString(R.string.save_success));
            finish();
        }
    }

    @Override
    protected void initData() {

    }
}